#include "GlobalHead.h"
#include "ViewProviderPathCamera.h"
#include "CustomPlayer.h"
#include "part/Part2DObject.h"
#include "part/PartPathCamera.h"
#include "Gui_Application.h"
#include "Command.h"
#include "CommandDraftTools.h"
#include "App/Document.h"
using namespace PartGui;
using namespace App;
PROPERTY_SOURCE(PartGui::ViewProviderPathCamera, PartGui::ViewProviderPart)

ViewProviderPathCamera::ViewProviderPathCamera(void)
:currentCurve(NULL)
{
	ADD_PROPERTY_TYPE(Animationlength,(5000.0f),"Animation",App::Prop_None,"The length of pathCamera");
	ADD_PROPERTY_TYPE(CameraAnimationPath,(long(0)),"Animation",App::Prop_None,"list of AnimationPath");
	//ADD_PROPERTY_TYPE(pCurLinkList,(0),"Loft",App::Prop_None,"List of sections");

	sPixmap = "PartFeatureImport";
}

ViewProviderPathCamera::~ViewProviderPathCamera(void)
{
	CameraAnimationPath.setEnums(0);
}
void ViewProviderPathCamera::attach(App::DocumentObject *pObject)
{

	attachDocument(pObject->getDocument());


	mTotalTime = Animationlength.getValue();

	if(pObject->getTypeId().isDerivedFrom(Part::PartPathCamera::getClassTypeId()))
	{
		Part::PartPathCamera* pppc = static_cast<Part::PartPathCamera*>(pObject);
		//if(pObject->isRestoring() == false)
		 updateCurContainter(true);
	}


	PartGui::ViewProviderPart::attach(pObject); 
}

void ViewProviderPathCamera::updateCurContainter(bool isAttach)
{
	charCurveNameList.clear();

	std::vector<DocumentObject*> objList = GetApp_Application().getActiveDocument()->getObjectsOfType(Part::Part2DObject::getClassTypeId());
    if(objList.empty())
		return;

	if(currentCurve == NULL)
      currentCurve = objList[0];


	std::vector<App::DocumentObject*>::iterator it = objList.begin();
	for(; it != objList.end(); it++)
	{
		charCurveNameList.push_back((*it)->getNameInDocument());
	}
	CameraAnimationPath.setEnumVector(charCurveNameList);

}

void ViewProviderPathCamera::removeCurve(App::DocumentObject* pObj)
{
	if(!pObj->getTypeId().isDerivedFrom(Part::Part2DObject::getClassTypeId()))
		return;

	charCurveNameList.clear();

	std::vector<DocumentObject*> objList = GetApp_Application().getActiveDocument()->getObjectsOfType(Part::Part2DObject::getClassTypeId());

	if(objList.empty())
		return;

	const char* objName = pObj->getNameInDocument();
	std::vector<App::DocumentObject*>::iterator it = objList.begin();
	for(; it != objList.end(); it++)
	{
		if(strcmp((*it)->getNameInDocument(),objName) != 0)
		charCurveNameList.push_back((*it)->getNameInDocument());
	}
	if(charCurveNameList.empty())
		CameraAnimationPath.setEnums(0);
	else
	CameraAnimationPath.setEnumVector(charCurveNameList);
}
void ViewProviderPathCamera::updateData(const App::Property*)
{

}
void ViewProviderPathCamera::onChanged(const App::Property* prop)
{
	if(prop == &CameraAnimationPath)
	{

		currentCurve = GetApp_Application().getActiveDocument()->getObject(CameraAnimationPath.getValueAsString());

		// 		 long ind = CameraAnimationPath.getValue();
		// 		 App::DocumentObject* pObject =  pCurLinkList.getValuebyIndex(ind);

		//        const char* vn =  CameraAnimationPath.getValueAsString();
		// 	   currentCurve = CCustomPlayer::Instance().getCurveByName(vn);

	}else if(prop == &Animationlength)
	{
		mTotalTime = Animationlength.getValue();
	}
	PartGui::ViewProviderPart::onChanged(prop);

}

bool ViewProviderPathCamera::setEdit(int ModNum)
{
	Command* pcom =  Gui_Application::instance->commandManager().getCommandByName("PathCamera");
	if(pcom)
	{
		ToolsPathCamera* pc = static_cast<ToolsPathCamera*>(pcom);
		pc->activateTools("PathCamera",pcObject);
	}
	return PartGui::ViewProviderPartExt::setEdit(ModNum);
}

void ViewProviderPathCamera::unsetEdit(int ModNum)
{
	Command* pcom =  Gui_Application::instance->commandManager().getCommandByName("PathCamera");
	if(pcom)
	{
		ToolsPathCamera* pc = static_cast<ToolsPathCamera*>(pcom);
		pc->deactivateTools("PathCamera");
	}
	PartGui::ViewProviderPartExt::unsetEdit(ModNum);
}

/** Checks if a new document was created */
void ViewProviderPathCamera::slotCreatedDocument(const App::Document& Doc)
{

}
/** Checks if the given document is about to be closed */
void ViewProviderPathCamera::slotDeletedDocument(const App::Document& Doc)
{

}
/** Checks if a new object was added. */
void ViewProviderPathCamera::slotCreatedObject(const App::DocumentObject& Obj)
{
	updateCurContainter(false);	
}
/** Checks if the given object is about to be removed. */
void ViewProviderPathCamera::slotDeletedObject(const App::DocumentObject& Obj)
{
	removeCurve(const_cast<App::DocumentObject*>(&Obj));
}
/** The property of an observed object has changed */
void ViewProviderPathCamera::slotChangedObject(const App::DocumentObject& Obj, const App::Property& Prop)
{
}


